package com.sskj.market.ui.activity;

import android.annotation.SuppressLint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.NumberUtil;
import com.sskj.market.R;
import com.sskj.market.R2;
import com.sskj.market.bean.HotShoppingDataBean;
import com.sskj.market.bean.ShoppingBean;
import com.sskj.market.http.HttpConfig;
import com.sskj.market.presenter.HotCommodityPresenter;
import com.sskj.market.ui.ShoppingActivity;
import com.sskj.market.util.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 热门商品Activity
 *
 * @author Hey
 * Create at  2019/07/25
 */
@Route(path = RConfig.MARKET_ACTIVITY_HOT_COMMODITY)
public class HotCommodityActivity extends BaseActivity<HotCommodityPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvReturn)
    TextView tvReturn;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.ivRight)
    ImageView ivRight;
    private int page = 1;
    List<HotShoppingDataBean.DataBean> data = new ArrayList<>();
    private BaseAdapter<HotShoppingDataBean.DataBean> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.market_activity_hot_commotidy;
    }

    @Override
    public HotCommodityPresenter getPresenter() {
        return new HotCommodityPresenter();
    }

    @Override
    public void initView() {
        mPresenter.getHotShoppingData(page, true);
        tvTitle.setText(App.INSTANCE.getString(R.string.market_overSeasActivity1));
        tvReturn.setVisibility(View.VISIBLE);

        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getHotShoppingData(page, false);
        });
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getHotShoppingData(page, false);
        });
    }

    @Override
    public void initData() {
        /*
         * recyclerview初始化
         */
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

        adapter = new BaseAdapter<HotShoppingDataBean.DataBean>(R.layout.activity_shopping_item, data, recyclerView, true) {
            @SuppressLint("CheckResult")
            @Override
            public void bind(ViewHolder holder, HotShoppingDataBean.DataBean item) {

                holder.setText(R.id.tvTitleShopItem, item.getName())
                        .setText(R.id.tvMoneyShopItem, NumberUtil.keep2(item.getPrice()) + "LAC")
                        .setText(R.id.tvNumberShopItem, item.getPurchase_number() + "LAC");
                ImageView ivShopItem = holder.getView(R.id.ivShopItem);

                if (item.getPics().size() != 0) {
                    GlideUtils.Show(item.getPics().get(0), ivShopItem);
                }

                LinearLayout linearLayout = holder.getView(R.id.llItem);
                ClickUtil.click(300, linearLayout, () -> {
                    ARouter.getInstance().build(RConfig.MARKET_ACTIVITY_COMMODITY_DETAILS).withInt("product_id", item.getId()).navigation();
                });
            }
        };
    }

    public void getHotShoppingDataSuccess(HotShoppingDataBean data) {
        if (data.getData() == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data.getData());
        } else {
            adapter.addData(data.getData());
        }
    }

    public void getHotShoppingRefreshSuccess() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }
}
